//
//  PulldownMenu.m
//  NemoOfficeClient
//
//  Created by 杨旭东 on 26/04/2017.
//  Copyright © 2017 ainemo. All rights reserved.
//

#import "PulldownMenu.h"
#import "UIColor+XYColor.h"

#pragma mark MenuDataItem

@implementation MenuDataItem

+ (NSArray *)shareItems:(NSArray *)items {
    NSMutableArray *array = [NSMutableArray array];
    
#if __clang_major__ > 10
    if (@available(iOS 11.0, *)) {
#else
    if ([UIDevice currentDevice].systemVersion.floatValue >= 11) {
#endif
        MenuDataItem *screen = [[MenuDataItem alloc] init];
        screen.title = @"屏幕";
        screen.canResponse = [items[0] boolValue];
        screen.tag = 10;
        [array addObject:screen];
    }
    
    
    MenuDataItem *photo = [[MenuDataItem alloc] init];
    photo.title = @"图片";
    photo.canResponse = [items[1] boolValue];
    photo.tag = 11;
    [array addObject:photo];
    
    MenuDataItem *whiteBoard = [[MenuDataItem alloc] init];
    whiteBoard.canResponse = [items[2] boolValue];
    whiteBoard.title = @"白板";
    whiteBoard.tag = 12;
    [array addObject:whiteBoard];
    
    return array;
}

+ (NSArray *)moreItems:(NSArray *)items {
    NSMutableArray *array = [NSMutableArray array];
    MenuDataItem *keyboard = [[MenuDataItem alloc] init];
    keyboard.title = @"键盘";
    keyboard.canResponse = [items[0] boolValue];
    keyboard.tag = 1;
    [array addObject:keyboard];
    
    MenuDataItem *record = [[MenuDataItem alloc] init];
    NemoRecordingType type = (NemoRecordingType)[items[1] integerValue];
    record.title = type==NemoRecordingTypeBySelf?@"结束录制":@"录制";
    record.canResponse = type==NemoRecordingTypeByOthers?NO:YES;
    record.tag = 2;
    [array addObject:record];
    
    MenuDataItem *torch = [[MenuDataItem alloc] init];
    torch.title = @"手电筒";
    torch.canResponse = [items[2] boolValue];
    torch.tag = 3;
    [array addObject:torch];
    
    MenuDataItem *audio = [[MenuDataItem alloc] init];
    audio.title = @"语音模式";
    audio.canResponse = [items[3] boolValue];
    audio.tag = 4;
    [array addObject:audio];
    
    return array;
}

@end



#pragma mark MenuCell

@interface MenuCell ()

@property (nonatomic, strong) UIImageView *icon;

@property (nonatomic, strong) UILabel *title;

@property (nonatomic, assign) BOOL last;

@end

@implementation MenuCell

+ (MenuCell *)menuCellWithTableview:(UITableView *)tableview last:(BOOL)last dataItem:(MenuDataItem *)item {
    MenuCell *cell = [tableview dequeueReusableCellWithIdentifier:@"MenuCell"];
    if (!cell) {
        cell = [[MenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MenuCell"];
        cell.item = item;
        cell.last = last;
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)setItem:(MenuDataItem *)item{
    self.contentView.frame = CGRectMake(0, 0, 120, 38);
    self.backgroundColor = [UIColor clearColor];

    self.title = [[UILabel alloc] initWithFrame:CGRectMake(0 , 0, 120, 38)];
    self.title.textAlignment = NSTextAlignmentCenter;
    self.title.font = [UIFont systemFontOfSize:15];
    self.title.textColor = item.canResponse?[UIColor whiteColor]:[UIColor colorWithWhite:1 alpha:0.3];
    self.title.text = item.title;
    [self.contentView addSubview:self.title];
}

- (void)setLast:(BOOL)last{
    _last = last;
    
    if (!last) {
        CGFloat width = self.contentView.bounds.size.width - 30;
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15, self.contentView.bounds.size.height - .5f, width, .5f)];
        line.backgroundColor = [UIColor colorWithHexString:@"#E4E4E4"];
        [self.contentView addSubview:line];
    }
}

@end



#pragma mark PulldownMenu

#define SCREENBOUNDS [UIScreen mainScreen].bounds

@interface PulldownMenu () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIImageView *backgroundView;

@property (nonatomic, assign) CGSize *size;

@property (nonatomic, strong) NSArray *dataSource;

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, copy) void (^selectedBlock)(NSInteger, MenuDataItem *);

@property (nonatomic, copy) void (^dismissBlock)();

@end

@implementation PulldownMenu

+ (PulldownMenu *)pulldownMenuDataSource:(NSArray<MenuDataItem *> *)items
                                selected:(void (^)(NSInteger, MenuDataItem *))selected
                                 dismiss:(void (^)())dismiss{
    PulldownMenu *menu = [[PulldownMenu alloc] initWithDataSource:items];
    menu.selectedBlock = selected;
    menu.dismissBlock = dismiss;
    return menu;
}

- (instancetype)initWithDataSource:(NSArray *)items{
    if (self = [super init]) {
        self.dataSource = items;
        self.frame = CGRectMake(0, 0, 120, items.count * 38.0);
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
        [self configSubviews];
    }
    return self;
}

- (void)configSubviews {
    _tableview = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    _tableview.dataSource = self;
    _tableview.delegate = self;
    _tableview.backgroundColor = [UIColor clearColor];
    _tableview.bounces = NO;
    _tableview.showsVerticalScrollIndicator = NO;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:_tableview];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 38.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [MenuCell menuCellWithTableview:tableView last:(indexPath.row == self.dataSource.count - 1) dataItem:_dataSource[indexPath.row]];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MenuDataItem *item = (MenuDataItem *)[_dataSource objectAtIndex:indexPath.row];
    if (item.canResponse) {
        if (self.selectedBlock) {
            self.selectedBlock(indexPath.row, [_dataSource objectAtIndex:indexPath.row]);
        }
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.dismissBlock();
}


@end
